//
//  BNHomeController.m
//  BNCodeGuide
//
//  Created by kevin007 on 2020/10/9.
//  Copyright © 2020 kevin007. All rights reserved.
//

#import "BNHomeController.h"

#import "BNHomeListView.h"   //table list View
#import "BNFooterButtonView.h" //footer button view

#import "BNHomeDetailController.h" //detail page

@interface BNHomeController ()<BNFooterButtonViewDelegate>
/** table list view*/
@property (nonatomic, strong) BNHomeListView *listView;

/** footer button view*/
@property (nonatomic, strong) BNFooterButtonView *footerButtonView;

/** floatButton*/
@property (nonatomic, strong) UIButton *floatButton;

/** datas*/
@property (nonatomic, strong) NSArray *datas;
@end

@implementation BNHomeController

#pragma mark - LifeCycle
- (void)dealloc{
    NSLog(@"%s", __func__);
}

- (void)viewDidLoad{
    [super viewDidLoad];
   
   
    [self setupUI];
//    [self requestData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //DO Something.
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    //DO Something.
}

#pragma mark - Public Methods
//对外暴露方法区

#pragma mark - Private Methods
//内部私有方法区

#pragma mark - SetupUI
- (void)setupUI {
    //导航栏样式，背景颜色处理
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    //添加子视图
    [self.view addSubview:self.listView];
    [self.view addSubview:self.floatButton];
    
    //约束布局
    [self addLayouts];
}

#pragma mark - addLayouts
- (void)addLayouts{
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(UIScreen.statusBarHeight);
        make.right.left.mas_offset(0);
        make.bottom.mas_equalTo(-UIScreen.tabbarHeight);
    }];
    
    [self.floatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-20);
        make.bottom.mas_offset(-UIScreen.tabbarHeight - 50);
        make.height.width.mas_offset(50);
    }];
    
}

#pragma mark - ----------------Request----------------
- (void)requestData {
    //test
    NSMutableArray *datas = @[].mutableCopy;
    int i = 0;
    while (i < 50) {
        [datas addObject:@(i)];
        i++;
    }
    
    self.datas = datas;
    [self refreshUI];
  
}

#pragma mark - ----------------refreshUI----------------
- (void)refreshUI {
    self.listView.dataSource = self.datas;
    [self.listView refreshUI];
}

#pragma mark - Inherit
//继承方法区
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    [self.floatButton setCornerRadiusWithRadius:25 corner:UIRectCornerAllCorners];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self.listView layoutIfNeeded];
        [self.listView setNeedsLayout];
        
        [self scrollToBottom:NO];
    });
}

#pragma mark - Events
- (void)buttonAction:(UIButton *)button {
    if (button.isSelected) {
        [self scrollToBottom:YES];
    } else {
        [self scrollToTop];
    }
    button.selected = !button.isSelected;
}

//滚动值顶部
-(void)scrollToTop {
    [self.listView.tableView bn_scrollToTopAnimated:YES];
}

//滚动值底部
- (void)scrollToBottom:(BOOL)animated {
    [self.listView.tableView bn_scrollToBottomAnimated:animated];
}

#pragma mark - 跳转至详情页
- (void)segueToHomeDetailPage{
    BNHomeDetailController *page = [BNHomeDetailController new];
    page.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:page animated:YES];
}


#pragma mark - BNFooterButtonViewDelegate
- (void)footerButtonDidTouchDowm:(BNFooterButtonView *)footerButtonView{
     NSLog(@"%s", __func__);
}

#pragma mark - Getter & Setter
- (BNHomeListView *)listView {
    if (!_listView) {
        _listView = [[BNHomeListView alloc] initWithFrame:CGRectZero tableViewStyle:UITableViewStylePlain];
        _listView.tableView.rowHeight = 50;
        _listView.tableView.tableFooterView = self.footerButtonView;
        if (@available(iOS 11.0, *)) {
            _listView.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }

        __weak typeof(self) weakSelf = self;
        _listView.didSelectRowAtIndexPathHandler = ^(NSIndexPath *indexPath) {
            NSLog(@"%s", __func__);
            __strong typeof(self) strongSelf = weakSelf;
            [strongSelf segueToHomeDetailPage];
            
        };
    }
    
    return _listView;
}

- (BNFooterButtonView *)footerButtonView{
    if (!_footerButtonView) {
        _footerButtonView = [BNFooterButtonView loadViewFromXib];
        _footerButtonView.autoresizingMask = UIViewAutoresizingNone;
        _footerButtonView.frame = CGRectMake(0, 0, UIScreen.width, 86);
        _footerButtonView.delegate = self;
        
    }
    return _footerButtonView;
}

- (UIButton *)floatButton {
    if (!_floatButton) {
        _floatButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_floatButton setTitle:@"↑" forState:UIControlStateNormal];
        [_floatButton setTitle:@"↓" forState:UIControlStateSelected];
        [_floatButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_floatButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        _floatButton.backgroundColor = [UIColor blueColor];
        [_floatButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        _floatButton.selected = YES;
    }
    
    return _floatButton;
}
@end
